using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public enum SUB_ATTRIBUTES{
	INVENTORY_SLOTS=0,
	MOVEMENT_SPEED=1,
	HIT_POINTS=2,
	EQUIP_TIME=3,
	NOISE_FACTOR=4,
	STAMINA=5,
	NONE
}

public class DependencyPair{
	public DependencyPair(Attribute attrib, float factor){
		baseAttribute = attrib;
		dependecyFactor = factor;
	}
	public Attribute baseAttribute;
	public float dependecyFactor;
}
public class SubAttributte{
	//Valores por default de los atributos secundarios.	
	private float[] defaultValues = new float[]{10,1,100,1,20,100};
	
	private Attribute[] dependencyAttributes;
	private float[] dependencyFactor;
	private SUB_ATTRIBUTES type;
	
	private float baseValue;
	private float bonusValue;
	private float actualValue;
	private float percentageBonus;
	private float maxValue;
	private bool applyDependenciesBonus;
	
	public void UpdateMaxValue(){
		float prevMaxValue = maxValue;
		maxValue = this.baseValue + this.bonusValue;	
					
		if (applyDependenciesBonus){
			if (dependencyAttributes != null){
				for (int i=0; i<dependencyAttributes.Length; i++){
					maxValue += dependencyFactor[i]*dependencyAttributes[i].GetTotalValue();
				}
			}
		}
				
		maxValue *= 1 + (percentageBonus/100);
		actualValue += (maxValue-prevMaxValue);
	}
	public void Refill(){
		this.actualValue = this.maxValue;	
	}
	public SubAttributte(SUB_ATTRIBUTES subAttribute){
		//this.type = type;
		baseValue = defaultValues[(int)subAttribute];
		bonusValue = 0;
		dependencyAttributes = null;
		UpdateMaxValue();
		actualValue = maxValue;
		applyDependenciesBonus = true;
	}
	public void ApplyDependenciesBonus(bool val){
		this.applyDependenciesBonus = val;	
		UpdateMaxValue();
	}
	public float GetDependenciesBonus(){
		if (dependencyAttributes == null)
			return 0;
		float bonusTotal = 0; 
		for (int i=0; i<dependencyAttributes.Length; i++){
			bonusTotal += dependencyAttributes[i].GetTotalValue()*dependencyFactor[i];
		}
		return bonusTotal;
	}
	public void SetDependencies(params DependencyPair[] dependecies){
		dependencyAttributes = new Attribute[dependecies.Length];
		dependencyFactor = new float[dependecies.Length];
		
		for (int i=0; i<dependecies.Length; i++){
			dependencyAttributes[i] = dependecies[i].baseAttribute;
			dependencyFactor[i] = dependecies[i].dependecyFactor;
		}
		UpdateMaxValue();
	}
	public void SetInitialValue(float val){
		baseValue = val;
		UpdateMaxValue();
	}	
	public void RaiseBaseValue(float ammount){
		baseValue += ammount;
		UpdateMaxValue();
	}
	public float GetBaseValue(){ return baseValue;}
	public void AddToBonus(float ammount){ 
		bonusValue+=ammount; 
		if (bonusValue<0)
			bonusValue = 0;
		UpdateMaxValue();
	}
	public float GetBonusValue(){ return bonusValue;}
	public void AddToActualValue(float ammount){
		actualValue	+= ammount;
		if (actualValue > maxValue)
			actualValue = maxValue;
		if (actualValue < 0)
			actualValue = 0;
	}
	public float GetActualValue(){return actualValue;}
	public void AddToPercetageBonus(float ammount){
		percentageBonus += ammount;
		UpdateMaxValue();
	}
	public float GetPercentageBonus(){return percentageBonus;}
	public float GetTotalValue(){return maxValue;}
	public float[] ToArrayData(){
		return new float[5]{actualValue,baseValue,bonusValue,maxValue,percentageBonus};	
	}
	public void SetArrayData(float[] values){
		this.actualValue = values[0];
		this.baseValue = values[1];
		this.bonusValue = values[2];
		this.maxValue = values[3];
		this.percentageBonus = values[4];
		UpdateMaxValue();
	}
}
